﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace Data
{
    [DataContract]
    public class User
    {
        #region Serializable
        [DataMember]
        protected String _name;
        [DataMember]
        private Boolean _isAdmin = false; 
        #endregion


        #region Объектная модель
        static protected readonly Dictionary<String, User> _allUsers = new Dictionary<String, User>();
        static public Dictionary<String, User> AllUsers { get { return _allUsers; } }

        /// <summary>
        /// Метод, который добавляет User в _allUsers после его получения с сервера
        /// </summary>
        /// <param name="us">Вносится в _allUsers</param>
        static public void AddToAllUsers(User us)
        {
            if (null != us)
            {
                if (!_allUsers.Keys.Contains(us.Name))
                {
                    _allUsers.Add(us.Name, us);
                }
            }
        }

        public String Name
        {
            get { return _name; }
        }

        public Boolean IsAdmin
        {
            get { return _isAdmin; }
        }

        static public User GetByName(String name)
        {
            return _allUsers[name];
        }
        #endregion
        
        static private readonly String _tableName = "[users]";
        static public String TableName
        {
            get { return _tableName; }
        }

        public User(SqlDataReader reader)
        {
            reader.Read();
            _name = (String)reader["id"];
            _isAdmin = (Boolean)reader["admin"];
            reader.Close();

            _allUsers.Add(_name, this);
        }

        static public DataTable AllUsersFromDB
        {
            get
            {
                DataTable dt = new DataTable();
                String query = "SELECT * FROM [users]";

                // Connect to the database and run the query.        
                SqlDataAdapter adapter = new SqlDataAdapter(query, DBManager.Instance.Connection);

                // Fill the DataSet.
                adapter.Fill(dt);

                return dt;
            }
            set
            {
                DataTable dt = new DataTable();
                String query = "SELECT * FROM [users]";

                // Connect to the database and run the query.        
                SqlDataAdapter adapter = new SqlDataAdapter(query, DBManager.Instance.Connection);
                // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);

                // Fill the DataSet.
                adapter.Update(value);
            }

        }


    }
}
